package com.trump.baiduim.controller;

import com.alibaba.fastjson.JSONObject;
import com.trump.baiduim.model.Message;
import com.trump.baiduim.model.User;
import com.trump.baiduim.service.AdminUser;
import com.trump.baiduim.utils.EncryptUtil;
import com.trump.baiduim.utils.ErrorCode;
import com.trump.baiduim.utils.ResultUtil;
import com.trump.baiduim.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author guoyuans & 11744
 */
@RestController
@Slf4j
@RequestMapping(value = "/api")
public class AdminController {
    
    /**
     * token
     */
    private String token = "";
    
    @Autowired
    private AdminUser adminUser;
    
    @Autowired
    private ResultUtil resultUtil;
    
    @Autowired
    private TokenUtil tokenUtil;
    
    @Autowired
    private EncryptUtil encryptUtil;

    /**
     * 登陆,后台登陆未对user_status进行在线、离线状态变更
     * @param userAccount
     * @param userPassword
     * @param response
     * @return
     */
    @RequestMapping(value = "/system/login",method = RequestMethod.POST)
    public ResultUtil AdminLogin(@RequestParam("userAccount")String userAccount, @RequestParam("userPassword")String userPassword,
                             HttpServletResponse response){
        try{
            System.out.println(encryptUtil.encryptPassword(userPassword));
            User user = adminUser.getUserByAccountAndPassword(userAccount, encryptUtil.encryptPassword(userPassword));
            ResultUtil resultUtil = this.resultUtil.ofSuccess();
            //生成token
            if (user != null && user.getUserAccount()!= null){
                String token  = tokenUtil.token(user.getUserAccount(),user.getUserPassword());
                Cookie cookie = new Cookie("token",token);
                cookie.setMaxAge(60 * 60 * 24 * 15);
                response.addCookie(cookie);
                resultUtil.setToken(token);
                return  resultUtil.ofSuccess();
            }else {
                return resultUtil.ofFail(ErrorCode.LOGIN_FAIL);
            }
        }catch (Exception e){
            log.error("调用接口失败",e);
            return null;
        }

    }

    /**
     * 注销
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/system/logout",method = RequestMethod.POST)
    public ResultUtil AdminLogout(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("userAccount");
        Cookie cookie = new Cookie("token",null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return resultUtil.ofSuccess();
    }
    
    @RequestMapping(value = "/system/queryAllUser",method = RequestMethod.POST)
    public ResultUtil getAllUserInfo(@RequestBody Map<String,String> body) {
        /**
         * 页码数
         */
        Integer pageNum = 1;
        /**
         * 每页显示条目数
         */
        Integer pageSize = 15;
        try {
            if (body.containsKey("pageNum")) {
                pageNum = Integer.valueOf(body.get("pageNum"));
            }
            
            if (body.containsKey("pageSize")) {
                pageSize = Integer.valueOf(body.get("pageSize"));
            }
            
            List<User> data = adminUser.getAllUserInfo(pageNum,pageSize);
            return resultUtil.ofSuccess(data);
        } catch (Exception e) {
            log.error("queryAllUser调用接口失败",e);
            return resultUtil.ofFail(500,"调用查找用户信息接口失败");
        }
    }
    
    @RequestMapping(value = "/system/queryMsg",method = RequestMethod.POST)
    public ResultUtil queryMessage(@RequestBody Map<String,String> body) {
        /**
         * 页码数
         */
        Integer pageNum = 1;
        /**
         * 每页显示条目数
         */
        Integer pageSize = 15;
       
        try {
            if (body.containsKey("pageNum")) {
                pageNum = Integer.valueOf(body.get("pageNum"));
            }
            if (body.containsKey("pageSize")) {
                pageSize = Integer.valueOf(body.get("pageSize"));
            }
            
            Message msg = JSONObject.parseObject(JSONObject.toJSONString(body),Message.class);
            
            List<Message> data = adminUser.queryMessage(pageNum,pageSize,msg);
            return resultUtil.ofSuccess(data);
        } catch (Exception e) {
            log.error("queryMsg调用失败",e);
            return resultUtil.ofFail(500,"调用查询历史记录接口失败");
        }
    }

    @RequestMapping(value = "/system/deleteUser", method = RequestMethod.POST)
    public ResultUtil deleteUserInfoByUserAccount(@RequestBody Map<String, String> body) {
        
        try {
            String user_account = "";
            if (body.containsKey("userAccount")) {
                user_account = body.get("userAccount");
            }

            adminUser.deleteUserInfoByUserAccount(user_account);
            return resultUtil.ofSuccess(null);
        } catch (Exception e) {
            log.error("调用deleteUser接口失败", e);
            return resultUtil.ofFail(500,"调用删除用户接口失败");
        }
    }

    @RequestMapping(value = "/system/updateUser", method = RequestMethod.POST)
    public ResultUtil updateUserInfo(@RequestBody Map<String, String> body) {
        
        try {
            if (! body.containsKey("userAccount")) {
                return resultUtil.ofFail(500,"请输入要修改的用户工号");
            }
            User user = JSONObject.parseObject(JSONObject.toJSONString(body), User.class);
            if (body.containsKey("userPassword")) {
                user.setUserPassword(encryptUtil.encryptPassword(body.get("userPassword")));
            }
            
            adminUser.updateUserInfo(user);
            return resultUtil.ofSuccess(null);
        } catch (Exception e) {
            log.error("调用updateUserinformation接口失败", e);
            return resultUtil.ofFail(500,"调用更新用户信息接口失败");
        }
    }
}
